﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using SPCFMedicineInformer.Members;
using umbraco.NodeFactory;
using System.Text;
using SPCFMedicineInformer.Helpers;
using SPCFMedicineInformer.Constants;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class ContentAccount_ConsultationDetails : System.Web.UI.MasterPage
    {
        int id = 0;
        string user = string.Empty;
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            int tempInt;
            string strId = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            id = int.TryParse(strId, out tempInt) ? tempInt : 0;
            ShowContent(false);
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    var User = HttpContext.Current.User.Identity;
                    var member = MemberHelper.GetMemberFromEmail(User.Name) as WebUser;
                    Node n = new Node(id);
                    var c = n.NodeTypeAlias == "Consultation" ? ContentHelper.GetByNodeId<DocumentTypes.Consultation>(id) : null;
                    if (c != null && member != null && c.UserId == member.Id.ToString())
                    {
                        TransferDetailsFromUmbraco(c);
                        ShowContent(true);
                    }
                }
            }
        }
        void ShowContent(bool b)
        {
            plcContent.Visible = b;
            plcErrorHolder.Visible = !b;
        }
        void TransferDetailsFromUmbraco(DocumentTypes.Consultation c)
        {
            litDateOfConsultation.Text = c.DateOfConsultation;
            litConsultantName.Text = c.ConsultantName;
            litGender.Text = c.Gender;
            litAge.Text = c.Age;
            litAddress.Text = c.Address;
            litDateOfBirth.Text = c.DateOfBirth;
            litHasAllergy.Text = c.HasAllergyWithMedicine.ToLower() == "0" ? "No" : c.HasAllergyWithMedicine.ToLower() == "1" ? "Yes" : c.HasAllergyWithMedicine;
            bool hasAllergies = c.HasAllergyWithMedicine.ToLower() == "yes" || c.HasAllergyWithMedicine.ToLower() == "1"; 

            litAllergyName.Text = hasAllergies ? FormHelper.ParseIdStringToNodeName(c.AllergyName, ", ") : c.AllergyName;
            litSymptoms.Text = ParseIdToName(c.Symptoms);

            litSuggestedMedcines.Text = ParseIdToLink(c.PossibleMedicines, "", c.Symptoms,hasAllergies ? c.AllergyName : "", c.Age);
            
        }
        string ParseIdToName(string strId)
        {
            var sb = new StringBuilder();
            var format = "<span class=\"coolBorder defaultCursor\">{0}<div class=\"content\" style=\"display:none;\">{1}</div></span>";
            var formatwidLink = "<span class=\"coolBorder\"><a href=\"{0}\">{1}</a><div class=\"content\" style=\"display:none;\">{2}</div></span>";
            var list = strId.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            int tempInt;
            int id;
            foreach (var item in list)
            {
                id = int.TryParse(item,out tempInt) ? tempInt : 0;
                Node n = new Node(id);
                if (n != null && !string.IsNullOrEmpty(n.Name))
                {
                    string link = n.GetProperty(UmbracoPropertyName.SYMPTOM_LINK).ToString();
                    if (!string.IsNullOrEmpty(link))
                    {
                        sb.AppendFormat(formatwidLink, link, n.Name,n.GetProperty(UmbracoPropertyName.SYMPTOM_DESCRIPTION).Value);
                    }
                    else
                    {
                        sb.AppendFormat(format, n.Name, n.GetProperty(UmbracoPropertyName.SYMPTOM_DESCRIPTION).Value);
                    }
                    
                }
            }
            return sb.ToString();
        }
        string ParseIdToLink(string strId,string strName,string chosenSymptoms,string allergies,string age)
        {
            var sb = new StringBuilder();
            var list = strId.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var listName = strName.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries).ToList();
            var formatwidLink = "<span class=\"coolBorder\"><a href=\"/condition-listing/condition/?{0}\">{1}</a><div class=\"content\" style=\"display:none;\"><img src=\"{2}\" height=\"100\" width=\"200\"/>{3}</div></span>";
            //var formatnoLink = "<span class=\"coolBorder defaultCursor\">{0}</span>";
            int tempInt;
            int id;
            int ctr = 0;
            foreach (var item in list)
            {
                id = int.TryParse(item,out tempInt) ? tempInt : 0;
                Node n = new Node(id);
                if (n != null && !string.IsNullOrEmpty(n.Name))
                {
                    var strLink = string.Concat("id=", n.Id, "&cs=", chosenSymptoms,"&am=",allergies,"&age=",age);
                    sb.AppendFormat(formatwidLink, strLink, n.Name, n.GetProperty(UmbracoPropertyName.IMAGE_URL).Value,n.GetProperty(UmbracoPropertyName.CONDITION_DESCRIPTION).Value);
                }
                //else
                //{
                //    sb.AppendLine(string.Format(formatnoLink, listName[ctr]));
                //}
                ctr++;
            }
            return sb.ToString();

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/account/my-consultations");
        }
    }
}